숫자형 데이터 증감
✒️ 2026-07-01 20:04 내용 수정
실습 참고 자료
- 인프런의 lettuce를 이용하여 redis 다루어 보기(저자: 신현호) 강의 내용을 참고하여 진행하였다.
- SpringBoot와 Redis 연결의 테스트 코드에서 추가로 진행한다.
package myproject.redis.lettuce.string;
import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import org.junit.jupiter.api.Test;
import java.time.Duration;
public class RedisLettuceString {
@Test
public void incrDecr() {
// host 주소
String host = "localhost";
// Redis 연결 주소 생성
RedisURI redisURI = RedisURI.builder()
.withHost(host)
.withPort(6379) // 포트 번호. Docker에서 설정한 값과 동일하게 설정
.withDatabase(0) // 0 - 15까지 존재
.build();
// Redis 클라이언트 생성
RedisClient redisClient = RedisClient.create(redisURI);
// Connection 연결
StatefulRedisConnection<String, String> connection = redisClient.connect();
// Redis 명령어
RedisCommands<String, String> redisCommands = connection.sync();
// Redis 연결 테스트를 위한 Key-value
String key = "lettuce:string";
String value = "hello";
// Redis에 Key-value 저장
redisCommands.set(key, value);
// 저장된 Key로 value 확인
String get = redisCommands.get(key);
System.out.println("get = " + get);
// DB 사용이 완료되면 connection을 끊고 Client도 종료한다.
connection.close();
redisClient.shutdown();
}
}
incr 메서드
- key를 생성하고 value에 아무 값도 넣지 않은 상태에서
incr()메서드를 호출하면 정수가 저장되어 값이 증가한다.
@Test
public void incrDecr() {
// 생략
// Redis 연결 테스트를 위한 Key-value
String key = "lettuce:string";
String value = "hello";
// incr, decr
Long incr = redisCommands.incr(key);
System.out.println("incr = " + incr);
// 생략
}


- key에 String을 저장했다면
incr()메서드 호출 시 value가 Integer가 아니거나 범위 밖이라는 에러 메시지가 뜬다.
@Test
public void incrDecr() {
// 생략
// Redis 연결 테스트를 위한 Key-value
String key = "lettuce:string";
String value = "hello";
// key에 String value 저장
redisCommands.set(key, value);
// incr, decr
Long incr = redisCommands.incr(key);
System.out.println("incr = " + incr);
// 생략
}

decr 메서드
decr()메서드를 호출하면 저장된 값에서 차감된 값이 뜬다.- 실행 전에 바로 위 단계에서 실행한 명령어로 key에 String value가 있으므로
redisCommands.flushdb()메서드를 사용하여 DB에 저장된 모든 key를 제거한 후 진행했다.
- 실행 전에 바로 위 단계에서 실행한 명령어로 key에 String value가 있으므로
@Test
public void incrDecr() {
// 생략
// Redis 연결 테스트를 위한 Key-value
String key = "lettuce:string";
String value = "hello";
// key에 String value 저장
redisCommands.set(key, value);
// incr
Long incr = redisCommands.incr(key);
System.out.println("incr = " + incr);
// decr
Long decr = redisCommands.decr(key);
System.out.println("decr = " + decr);
// 생략
}

incrby, decrby 메서드
incrby(K key, Long amount)와decrby(K key, Long amount)는 key에 저장된 값을 amount만큼 증가 혹은 감소 시킨다.
@Test
public void incrDecr() {
// 생략
// Redis 연결 테스트를 위한 Key-value
String key = "lettuce:string";
String value = "hello";
// key에 String value 저장
redisCommands.set(key, value);
// incr
Long incr = redisCommands.incr(key);
System.out.println("incr = " + incr);
// decr
Long decr = redisCommands.decr(key);
System.out.println("decr = " + decr);
// incrby
Long incrby = redisCommands.incrby(key, 10);
System.out.println("incrby = " + incrby);
// decrby
Long decrby = redisCommands.decrby(key, 20);
System.out.println("decrby = " + decrby);
// 생략
}
